<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/18/018
 * Time: 19:51
 */

namespace app\Lib\Ioc\Order;


use app\Lib\Exception\BlException;
use app\Lib\Model\Order\AddOrder;
use app\Lib\Model\Order\GetMySite;
use app\Lib\Model\Order\GetMySiteInfo;
use app\Lib\Model\Order\GetOrder;
use app\Lib\Model\Order\GetOrderLog;
use app\Lib\Model\Order\GetPicFile;
use app\Lib\Model\Order\HangStopWordTag;
use app\Lib\Model\Order\OperatingOrderLog;
use app\Lib\Model\Order\OptionOrderStartTime;
use app\Lib\Model\Order\Order;
use app\Lib\Model\Order\OrderSupervisor;
use app\Lib\Model\Order\UpdatePicFile;
use app\Lib\Model\Tag\OrderStopWordReason\StopWordTag;
use app\Lib\Model\Tag\OrderStopWordReason\StopWordTagTagSelectList;
use app\Lib\Model\User\User;
use think\Db;

class OrderOption
{
    /**
     * 我的工地
     * @param $data
     * @return array
     */
    public function getMySite($data){
        $model =  new GetMySite(new User($data['opUid']));
        return  $model->run();
    }

    /**
     * 停工理由
     * @return array
     */
    public function getStopWordReason(){
        $model = new StopWordTagTagSelectList();
        return $model->getList();
    }
    /**
     * 页数
     * @param $data
     * @return array|int
     */
    public function getPage($data){
        $model = new GetOrder(new User($data['opUid']));
        $model->setKeyword($data['keyword']);
        $model->setKeywordType($data['keywordType']);
        $model->setIsPage(true);
        $model->setOrderStatus($data['order_status']);
        return $model->run();
    }

    /**
     * 列表
     * @param $data
     * @return array|int
     */
    public function getList($data){
        $model = new GetOrder(new User($data['opUid']));
        $model->setKeyword($data['keyword']);
        $model->setKeywordType($data['keywordType']);
        $model->setOrderStatus($data['order_status']);
        $model->setLimit($data['limit']);
        $model->setPage($data['page']);
        return $model->run();
    }
    //下单
    public function addOrder($data){
        return Db::transaction(function ()use($data){
            $model =  (new AddOrder(new User($data['opUid'])));
            $model->setUserAddress($data['user_address']);
            $model->setDuration($data['duration']);
            $model->setHouseType($data['house_type']);
            $model->setIntroducerPhone($data['introducer_phone']);
            $model->setOrderMoney($data['order_money']);
            $model->setOrderStyle($data['order_style']);
            $model->setUserName($data['user_name']);
            $model->setUserPhone($data['user_phone']);
            $model->setDrawingUid($data['drawing_uid']);
            $model->setUserSex($data['user_sex']);
            $model->setDesign_uid($data['design_uid']);
            $model->run();
            return true;
        });
    }
    //选择施工经理
    public function selectSiteSupervisorUid($data){
        Db::transaction(function ()use ($data){
            $model =  (new OrderSupervisor(new User($data['opUid']),new Order($data['order_number'],false)));
            $model->setSupervisorUser(new User($data['supervisorUid']));
            $model->setReason($data['reason']);
            $model->selectSupervisor();
            return true;
        });
        return true;
    }

    //审核
    public function reviewSiteSupervisorUid($data){
        Db::transaction(function ()use ($data){
            $order = new Order($data['order_number']);
            $model =  (new OrderSupervisor(new User($data['opUid']),$order));
            $model->setReview($data['review']);
            if(isset($data['reason'])){
                $model->setReason($data['reason']);
            }
            if($data['status'] == 'Refuse'){
                $model->refuse();
            }else{
                $model->agree();
            }
            return true;
        });
        return true;
    }

    //选择开工日期
    public function choiceOrderStartTime($data){
        $order = new Order($data['order_number']);
        Db::transaction(function ()use ($data,$order){
            $model =  (new OptionOrderStartTime($order,new User($data['opUid'])));
            $model->setOrderStart($data['orderStartTime']);
            $model->choiceTime();
            return true;
        });
        return true;
    }

    //上传施工图 效果图 全景图
    public function updatePicFile($data){
        return Db::transaction(function ()use($data){
            $model =  new UpdatePicFile(new Order($data['order_number']),new User($data['opUser']));
            if(!empty($data['new_img_id'])){
                $model->setImgArray($data['new_img_id']);
            }
            if(!empty($data['url'])){
                $model->setPanoramicUrl($data['url']);
            }
            if(!empty($data['del_img_id'])){
                $model->setDelImgArray($data['del_img_id']);
            }
            return $model->run();
        });
    }


    public function getPicFile($data){
        return (new GetPicFile(new Order($data['order_number'])))->run();
    }
    //自动任务开工


    //工地日志回复
    /**
     * @param $data
     * @return bool
     */
    public function orderLog($data){

        return Db::transaction(function ()use ($data){
//            throw new BlException(json_encode($data));
            $file = json_decode($data['file'],true);
            $model =  (new  OperatingOrderLog(new Order($data['order_number']),new User($data['opUid'])));
            $model->setContent($data['content']);
            $model->setImgArray($file['img']);
            $model->run();
            return true;
        });
    }

    /**
     * @param $data
     * @return int|mixed|string
     * @throws \app\Lib\Exception\BlException
     */
    public function hangStopWordTag($data){
        $model = new HangStopWordTag(new User($data['opUid']), new StopWordTag($data['tagId']),new Order($data['order_number']));
        $model->setContent($data['content']);
        return $model->run();
    }

    //检测标签




    //监控工地日志回复
//    public function monitorOrderLog(){
//
//    }
    /**
     * 工单日志
     * @param $data
     * @return array
     * @throws BlException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMyOrderLog($data){
        $model = new GetMySiteInfo(new User($data['opUid']));
        if(isset($data['order_number'])){
            $model->setOrder(new Order($data['order_number']));
        }
        return  $model->run();
    }

    /**
     * @param $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAjaxOrderLog($data){
        $model = new GetOrderLog(new Order(['order_number']));
        $model->page($data['page']);
        $model->run();
    }
}